<template>
    <el-dialog
            :title="!dataForm.assetId ? '新增' : '修改'"
            :close-on-click-modal="false"
            :visible.sync="visible">
        <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="120px">
                            <el-form-item label="固定资产ID" prop="assetId">
                        <el-input v-model.number="dataForm.assetId" placeholder="固定资产ID"></el-input>
                </el-form-item>
                <el-form-item label="资产编号" prop="assetCode">
                        <el-input v-model="dataForm.assetCode" placeholder="资产编号"></el-input>
                </el-form-item>
                <el-form-item label="资产名称" prop="assetName">
                        <el-input v-model="dataForm.assetName" placeholder="资产名称"></el-input>
                </el-form-item>
                <el-form-item label="购置日期" prop="purchaseDate">
                        <el-date-picker
                                v-model="dataForm.purchaseDate"
                                type="datetime"
                                placeholder="购置日期">
                        </el-date-picker>
                </el-form-item>
                <el-form-item label="原值" prop="originalValue">
                        <el-input v-model="dataForm.originalValue" placeholder="原值"></el-input>
                </el-form-item>
                <el-form-item label="使用部门" prop="department">
                        <el-input v-model="dataForm.department" placeholder="使用部门"></el-input>
                </el-form-item>
                <el-form-item label="折旧方法" prop="depreciationMethod">
                        <el-input v-model="dataForm.depreciationMethod" placeholder="折旧方法"></el-input>
                </el-form-item>
                <el-form-item label="预计使用年限" prop="usefulLife">
                        <el-input v-model.number="dataForm.usefulLife" placeholder="预计使用年限"></el-input>
                </el-form-item>
                <el-form-item label="残值" prop="residualValue">
                        <el-input v-model="dataForm.residualValue" placeholder="残值"></el-input>
                </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
    </el-dialog>
</template>

<script>
    export default {
      data () {
        return {
          visible: false,
          dataForm: {
            assetId: null,
            assetCode: null,
            assetName: null,
            purchaseDate: null,
            originalValue: null,
            department: null,
            depreciationMethod: null,
            usefulLife: null,
            residualValue: null
          },
          dataRule: {
            assetId: [
                        { required: true, message: '固定资产ID不能为空', trigger: 'blur' }
            ],
            assetCode: [
                        { required: true, message: '资产编号不能为空', trigger: 'blur' }
            ],
            assetName: [
                        { required: true, message: '资产名称不能为空', trigger: 'blur' }
            ],
            purchaseDate: [
                        { required: true, message: '购置日期不能为空', trigger: 'blur' }
            ],
            originalValue: [
                        { required: true, message: '原值不能为空', trigger: 'blur' }
            ],
            department: [
                        { required: true, message: '使用部门不能为空', trigger: 'blur' }
            ],
            depreciationMethod: [
                        { required: true, message: '折旧方法不能为空', trigger: 'blur' }
            ],
            usefulLife: [
                        { required: true, message: '预计使用年限不能为空', trigger: 'blur' }
            ],
            residualValue: [
                        { required: true, message: '残值不能为空', trigger: 'blur' }
            ]
          }
        }
      },
      methods: {
        init (id) {
          this.dataForm.assetId = id || 0
          this.visible = true
          this.$nextTick(() => {
            this.$refs['dataForm'].resetFields()
            this.dataForm.assetId = id || 0
            if (this.dataForm.assetId) {
              this.$http({
                url: this.$http.adornUrl(`/app/fixed_assets/info/${this.dataForm.assetId}`),
                method: 'get',
                params: this.$http.adornParams()
              }).then(({data}) => {
                if (data && data.code === 0) {
                  this.dataForm = data.appfixedassets
                }
              })
            }
          })
        },
            // 表单提交
        dataFormSubmit () {
          this.$refs['dataForm'].validate((valid) => {
            if (valid) {
              this.$http({
                url: this.$http.adornUrl(`/app/fixed_assets/${!this.dataForm.assetId ? 'save' : 'update'}`),
                method: 'post',
                data: this.$http.adornData(this.dataForm)
              }).then(({data}) => {
                if (data && data.code === 0) {
                  this.$message({
                    message: '操作成功',
                    type: 'success',
                    duration: 1500,
                    onClose: () => {
                      this.visible = false
                      this.$emit('refreshDataList')
                    }
                  })
                } else {
                  this.$message.error(data.msg)
                }
              })
            }
          })
        }
      }
    }
</script>